package com.shixianchen.review_the_day_before.day4;

import com.shixianchen.utils.Utils;
import com.shixianjun.sort.Sortable;
import com.shixianjun.sort.SortableElement;

/**
 * @author ShiXianChen
 * @version V1.0.0
 * @Description 冒泡排序优化
 * @date 2022/11/3 23:12
 */
public class BubbleSort implements Sortable {
    @Override
    public void sort(SortableElement[] arr) {
        int n = arr.length;

        for (int i = 0; i < n; i++) {
            boolean flag = true;
            for (int j = 0; j < n-i-1; j++) {
                if (arr[j].value > arr[j + 1].value) {
                    flag = false;
                    Utils.swap(arr, j, j + 1);
                }
            }
            if (flag){
                break;
            }
        }
    }
}
